package com.test.fileapplication.base;


import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.test.fileapplication.util.RemoteFile;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.LayoutRes;

/**
 * Created by GaoTing on 2020/2/17.
 * <p>
 * Explain:
 */
public abstract class BaseMultiItemAdapter<T extends MultiItemEntity> extends BaseMultiItemQuickAdapter<T, BaseViewHolder> {


    protected OnItemClickInterface onItemClickInterface;//处理item的各种点击事件
    public OnItemClickInterface getonItemClickInterface() {
        return onItemClickInterface;
    }

    public void setonItemClickInterface(OnItemClickInterface onItemClickInterface) {
        this.onItemClickInterface = onItemClickInterface;
    }
    protected List<RemoteFile> checkedList = new ArrayList<> ();//选中的文件列表
    public List<RemoteFile> getcheckedList() {
        return checkedList;
    }

    public void setcheckedList(List<RemoteFile> checkedList) {
        this.checkedList = checkedList;
    }

    protected boolean isSelectMode = false;

    public boolean getisSelectMode() {
        return isSelectMode;
    }

    public void setisSelectMode(boolean isSelectMode) {
        this.isSelectMode = isSelectMode;
    }

    protected int fileNumber;//鏂囦欢鎬绘暟

    public int getfileNumber() {
        return fileNumber;
    }

    public void setfileNumber(int fileNumber) {
        this.fileNumber = fileNumber;
    }

    protected int column = 1;

    public int getcolumn() {
        return column;
    }

    public void setcolumn(int column) {
        this.column = column;
    }

    protected int collapse;

    public int getcollapse() {
        return collapse;
    }

    public void setcollapse(int collapse) {
        this.collapse = collapse;
    }

    public BaseMultiItemAdapter(List<T> data) {
        super(data);
        setSpanSizeLookup((gridLayoutManager, i) -> {
            column = gridLayoutManager.getSpanCount();
            if (getItem(i).getItemType() == 0) {
                return gridLayoutManager.getSpanCount();
            }
            return 1;
        });
//        setHasStableIds(true);
    }

    public BaseMultiItemAdapter(@LayoutRes int... layouts) {
        this(new ArrayList<>());
        setItemView(layouts);
    }

    public void setItemView(@LayoutRes int... layouts) {
        for (int i = 0; i < layouts.length; i++) {
            addItemType(i, layouts[i]);
        }
    }

    public int getFileNumber() {
        return fileNumber;
    }


    public void setListener(OnItemClickInterface onItemClickInterface) {
        this.onItemClickInterface = onItemClickInterface;
    }

    public void setSelectMode(boolean b) {
        this.isSelectMode = b;
        notifyDataSetChanged();
    }

    public boolean isSelectMode() {
        return isSelectMode;
    }

    public boolean isChecked(RemoteFile file) {
        return checkedList.contains(file);
    }

    public List<RemoteFile> getCheckedList() {
        return checkedList;
    }

    public void addCheckList(RemoteFile file) {
        if (!checkedList.contains(file)) {
            checkedList.add(file);
        }
        if (onItemClickInterface != null) {
            onItemClickInterface.selectCount(checkedList, checkedList.size());
        }
    }

    public void removeCheckList(RemoteFile file) {
        checkedList.remove(file);
        if (onItemClickInterface != null) {
            onItemClickInterface.selectCount(checkedList, checkedList.size());
        }
    }

    public void removeAllCheckedList() {
        checkedList.clear();
        if (onItemClickInterface != null) {
            onItemClickInterface.selectCount(checkedList, 0);
        }
    }

    @Override
    public long getItemId(int position) {

        return position ;
    }
}
